package com.miyake.dsp.osa.lab.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import org.apache.commons.lang.ArrayUtils;

import com.miyake.dsp.common.AdjustArraySize;
import com.miyake.dsp.common.CommonTool;
import com.miyake.dsp.common.UnitConverter;
import com.miyake.dsp.osa.lab.ChartSetting;
import com.miyake.dsp.osa.lab.Connector;
import com.miyake.dsp.osa.lab.LabContainer;
import com.miyake.dsp.osa.lab.wrapper.ChartWrapper;
import com.miyake.dsp.ui.LineChartPanel;

public class ChartOutputPanelUI extends OutletPanelUI {
	@Override
	public synchronized void updateData(String key, Connector con) {
		
		super.updateData(key, con);
		if (chart != null) {
			updateChart();
		}
	}


	private synchronized void updateChart() {
		//System.out.println("ChartOutputPanelUI updateChart");
		ChartSetting setting = (ChartSetting)this.getContainer().getSetting();
		
		Set<String> keySet = new TreeSet<String>(getContainer().getInConnectorKeySet());
		List<double[]> d = new ArrayList<double[]>();
		List<String> name = new ArrayList<String>();
		for (String key : keySet) {
			Connector c = getContainer().getInConnector(key);
			
			if (c != null && c.getData() != null && key.contains("#")) {
				d.add(c.getData().getRealPart());
				if (key.contains("y")) {
					name.add(key);
				}
			}
			else {
				d.add(null);
			}
		}
		
		Iterator<double[]> it = d.iterator();
		Iterator<String> itName = name.iterator();
		//chart.getChart().removeAllTrace();
		
		while (it.hasNext()) {
			double[] x = it.next();
			double[] y = null;
			try {
				y = it.next();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			if (x == null && y == null) {
				continue;
			}
			
			if (setting.getyAxisType().equals(ChartSetting.YAxisType.dB)) {
				y = UnitConverter.todB(y);
			}
			else if (setting.getyAxisType().equals(ChartSetting.YAxisType.dBm)) {
				y = UnitConverter.todBm(y);
			}
			
			if (x == null) {
				chart.getChart().setData(itName.next(), y);
			}
			else {
				AdjustArraySize a = new AdjustArraySize(x, y);
				chart.getChart().setData(itName.next(), a.getX1(), a.getX2());
			}
		}
		//System.out.println("removeAllTrace");
		Connector mx = getContainer().getInConnector(ChartWrapper.MARKER_X);
		if (mx != null && mx.getData() != null) {
			chart.getChart().addXMarker(mx.getData().getRealPart());
		}
		Connector my = getContainer().getInConnector(ChartWrapper.MARKER_Y);
		if (my != null && my.getData() != null) {
			double[] ymarker = my.getData().getRealPart();
			if (setting.getyAxisType().equals(ChartSetting.YAxisType.dB)) {
				ymarker = UnitConverter.todB(ymarker);
			}
			else if (setting.getyAxisType().equals(ChartSetting.YAxisType.dBm)) {
				ymarker = UnitConverter.todBm(ymarker);
			}
			chart.getChart().addYMarker(ymarker);
		}
	}

	ChartFrame chart;
	
	@Override
	protected void createList(String title, Map<String, Connector> cons,
			Set<String> keySet, boolean in) {
		if (in) {
			super.createList(title, cons, keySet, in);
		}
		else {
			JPanel panel = new JPanel();
			panel.setBorder(new TitledBorder(title));
			panel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
			add(panel);
			
			JButton button = new JButton("Chart");
			panel.add(button);
			
			button.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					showChart();
				}
				
			});
		}
	}

	
	protected void showChart() {
		if (chart == null) {
			chart = new ChartFrame("Chart");
		}
		updateChart();
		chart.setVisible(true);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 7743251289974598730L;

	public ChartOutputPanelUI(LabContainer container, UiPanelListener listener) {
		super(container, listener);
		
	}

}
